
package com.tools.ad.ui.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.tools.ad.R;


public class RoundImageView extends ImageView {

    private final Paint maskPaint = new Paint();
    private final Paint zonePaint = new Paint();
    private int mTopLeftRadius;
    private int mTopRightRadius;
    private int mBottomLeftRadius;
    private int mBottomRightRadius;
    private final RectF roundRect = new RectF();
    private Path mRoundPath = new Path();

    public RoundImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initFromAttributes(context, attrs);
        init();
    }

    public RoundImageView(Context context) {
        super(context);
        init();
    }

    private void init() {
        maskPaint.setAntiAlias(true);
        maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

        zonePaint.setAntiAlias(true);
        zonePaint.setColor(Color.WHITE);
    }

    private void initFromAttributes(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundImageView, 0, 0);
        int radius = a.getDimensionPixelSize(R.styleable.RoundImageView_ri_radius, 0);
        int topLeftRadius = a.getDimensionPixelSize(R.styleable.RoundImageView_ri_topLeftRadius, 0);
        int topRightRadius = a.getDimensionPixelSize(R.styleable.RoundImageView_ri_topRightRadius, 0);
        int bottomLeftRadius = a.getDimensionPixelSize(R.styleable.RoundImageView_ri_bottomLeftRadius, 0);
        int bottomRightRadius = a.getDimensionPixelSize(R.styleable.RoundImageView_ri_bottomRightRadius, 0);
        if (radius > 0) {
            setRadius(radius, radius, radius, radius);
        } else {
            setRadius(topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius);
        }
        a.recycle();
    }

    public void setRadius(int topLeftRadius, int topRightRadius, int bottomLeftRadius, int bottomRightRadius) {
        if (topLeftRadius >= 0) {
            mTopLeftRadius = topLeftRadius;
        } else {
            throw new IllegalArgumentException("topLeftRadius must be >= 0");
        }
        if (topRightRadius >= 0) {
            mTopRightRadius = topRightRadius;
        } else {
            throw new IllegalArgumentException("topRightRadius must be >= 0");
        }
        if (bottomLeftRadius >= 0) {
            mBottomLeftRadius = bottomLeftRadius;
        } else {
            throw new IllegalArgumentException("bottomLeftRadius must be >= 0");
        }
        if (bottomRightRadius >= 0) {
            mBottomRightRadius = bottomRightRadius;
        } else {
            throw new IllegalArgumentException("bottomRightRadius must be >= 0");
        }
        invalidate();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right,
                            int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        int w = getWidth();
        int h = getHeight();
        roundRect.set(0, 0, w, h);
    }

    @Override
    public void draw(Canvas canvas) {
        canvas.saveLayer(roundRect, zonePaint, Canvas.ALL_SAVE_FLAG);
        mRoundPath.reset();
        mRoundPath.addRoundRect(roundRect,
                new float[]{
                        mTopLeftRadius, mTopLeftRadius,
                        mTopRightRadius, mTopRightRadius,
                        mBottomLeftRadius, mBottomLeftRadius,
                        mBottomRightRadius, mBottomRightRadius
                },
                Path.Direction.CCW);
        canvas.drawPath(mRoundPath, zonePaint);
        canvas.saveLayer(roundRect, maskPaint, Canvas.ALL_SAVE_FLAG);
        super.draw(canvas);
        canvas.restore();
    }

}
